package com.xbongbong.parent.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.TemplateFieldEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.model.ContractEditFileModel;
import com.xbongbong.paas.model.ContractTemplateBaseModel;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.domain.entity.LabelEntity;
import com.xbongbong.pro.onlineEditor.TemplateFieldValuePojo;
import com.xbongbong.saas.constant.OnlineEditorConstant;
import com.xbongbong.saas.domain.entity.ContractEditFileEntity;
import com.xbongbong.saas.domain.entity.ContractTemplateBaseEntity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author: yingjf
 * @date: 2023/3/15 09:21
 * @description:
 */
@AllArgsConstructor
@Component
public class AnalysisTemplateFieldHelp {
    private ContractTemplateBaseModel contractTemplateBaseModel;
    private ContractEditFileModel contractEditFileModel;

    /**
     * 解析模板字段
     * date: 2023/3/14 17:16
     *
     * @param resultJsonObject
     * @param attr
     * @author yingjf
     */
    public void analysisTemplateField4TemplateName(String corpid, JSONObject resultJsonObject, String attr) {
        String jsonStr = resultJsonObject.getString(attr);
        TemplateFieldValuePojo templateFieldValuePojo = JSONObject.parseObject(jsonStr, TemplateFieldValuePojo.class);
        Long base_contract_id = templateFieldValuePojo.getBase_contract_id();
        String contract_edit_file_id = templateFieldValuePojo.getContract_edit_file_id();
        Integer contract_edit_file_version = templateFieldValuePojo.getContract_edit_file_version();
        if (base_contract_id != null) {
            ContractTemplateBaseEntity contractTemplateBaseEntity = contractTemplateBaseModel.getByKey(base_contract_id, corpid);
            if (contractTemplateBaseEntity != null) {
                templateFieldValuePojo.setBase_contract_name(contractTemplateBaseEntity.getTemplateName());
            }
        }
        if (contract_edit_file_id != null && contract_edit_file_version != null) {
            ContractEditFileEntity contractEditFileEntity = contractEditFileModel.getOneByFileIdAndFileVersion(corpid, contract_edit_file_id, contract_edit_file_version);
            if (contractEditFileEntity != null) {
                templateFieldValuePojo.setFile_url(contractEditFileEntity.getFileUrl());
                templateFieldValuePojo.setFile_name(contractEditFileEntity.getFileName());
                templateFieldValuePojo.setFile_ext(contractEditFileEntity.getFileExt());
                templateFieldValuePojo.setFile_size(contractEditFileEntity.getFileSize());
            }
        }
        resultJsonObject.put(attr, templateFieldValuePojo.getBase_contract_name());
    }


    /**
     * 解析模板字段
     * date: 2023/3/14 17:16
     *
     * @param resultJsonObject
     * @param attr
     * @author yingjf
     */
    public void analysisTemplateField(String corpid, JSONObject resultJsonObject, String attr) {
        String jsonStr = resultJsonObject.getString(attr);
        TemplateFieldValuePojo templateFieldValuePojo = JSONObject.parseObject(jsonStr, TemplateFieldValuePojo.class);
        Long base_contract_id = templateFieldValuePojo.getBase_contract_id();
        String contract_edit_file_id = templateFieldValuePojo.getContract_edit_file_id();
        Integer contract_edit_file_version = templateFieldValuePojo.getContract_edit_file_version();
        if (base_contract_id != null) {
            ContractTemplateBaseEntity contractTemplateBaseEntity = contractTemplateBaseModel.getByKey(base_contract_id, corpid);
            if (contractTemplateBaseEntity != null) {
                templateFieldValuePojo.setBase_contract_name(contractTemplateBaseEntity.getTemplateName());
            }
        }
        if (contract_edit_file_id != null && contract_edit_file_version != null) {
            ContractEditFileEntity contractEditFileEntity = contractEditFileModel.getOneByFileIdAndFileVersion(corpid, contract_edit_file_id, contract_edit_file_version);
            if (contractEditFileEntity != null) {
                templateFieldValuePojo.setFile_url(contractEditFileEntity.getFileUrl());
                templateFieldValuePojo.setFile_name(contractEditFileEntity.getFileName());
                templateFieldValuePojo.setFile_ext(contractEditFileEntity.getFileExt());
                templateFieldValuePojo.setFile_size(contractEditFileEntity.getFileSize());
            }
        }
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put(OnlineEditorConstant.BASE_CONTRACT_NAME, templateFieldValuePojo.getBase_contract_name());
        map.put(OnlineEditorConstant.BASE_CONTRACT_ID, templateFieldValuePojo.getBase_contract_id());
        map.put(OnlineEditorConstant.FILE_URL, templateFieldValuePojo.getFile_url());
        map.put(OnlineEditorConstant.FILE_NAME, templateFieldValuePojo.getFile_name());
        map.put(OnlineEditorConstant.FILE_EXT, templateFieldValuePojo.getFile_ext());
        map.put(OnlineEditorConstant.FILE_SIZE, templateFieldValuePojo.getFile_size());
        map.put(OnlineEditorConstant.CONTRACT_EDIT_FILE_ID, templateFieldValuePojo.getContract_edit_file_id());
        map.put(OnlineEditorConstant.CONTRACT_EDIT_FILE_VERSION, templateFieldValuePojo.getContract_edit_file_version());
        resultJsonObject.put(attr, map);
    }

    /**
     * 列表解析
     * date: 2023/4/4 17:29
     * @author yingjf
     * @param resultJsonObject
     * @param attr
     * @param templateFieldMap
     */
    public void analysisTemplateField4Map( JSONObject resultJsonObject, String attr,Map<Object, TemplateFieldValuePojo> templateFieldMap) {
        String jsonStr = resultJsonObject.getString(attr);
        TemplateFieldValuePojo templateFieldValuePojo = JSONObject.parseObject(jsonStr, TemplateFieldValuePojo.class);
        Long base_contract_id = templateFieldValuePojo.getBase_contract_id();
        String contract_edit_file_id = templateFieldValuePojo.getContract_edit_file_id();
        Integer contract_edit_file_version = templateFieldValuePojo.getContract_edit_file_version();
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (base_contract_id != null) {
            TemplateFieldValuePojo temp = templateFieldMap.get(base_contract_id);
            map.put(OnlineEditorConstant.BASE_CONTRACT_NAME, temp.getBase_contract_name());
            map.put(OnlineEditorConstant.BASE_CONTRACT_ID, temp.getBase_contract_id());
        }
        if (contract_edit_file_id != null && contract_edit_file_version != null) {
            String key = contract_edit_file_id + contract_edit_file_version;
            TemplateFieldValuePojo temp = templateFieldMap.get(key);
            map.put(OnlineEditorConstant.FILE_URL, temp.getFile_url());
            map.put(OnlineEditorConstant.FILE_NAME, temp.getFile_name());
            map.put(OnlineEditorConstant.FILE_EXT, temp.getFile_ext());
            map.put(OnlineEditorConstant.FILE_SIZE, temp.getFile_size());
            map.put(OnlineEditorConstant.CONTRACT_EDIT_FILE_ID, temp.getContract_edit_file_id());
            map.put(OnlineEditorConstant.CONTRACT_EDIT_FILE_VERSION, temp.getContract_edit_file_version());
        }
        resultJsonObject.put(attr, map);
    }


    /**
     * 列表解析 前置批量获取数据
     * date: 2023/4/4 17:29
     * @author yingjf
     * @param corpid
     * @param headList
     * @param formDataEsList
     */
    public Map<Object, TemplateFieldValuePojo> getTemplateFieldMap(String corpid, List<FieldAttrEntity> headList, List<PaasFormDataEntityExt> formDataEsList) {
        Map<Object, TemplateFieldValuePojo> templateFieldMap = new HashMap<>();
        String templateFieldAttr = null;
        for (FieldAttrEntity fieldAttrEntity : headList) {
            if (Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.TEMPLATE_FIELD.getType())) {
                templateFieldAttr = fieldAttrEntity.getAttr();
                break;
            }
        }
        if (StringUtil.isNotEmpty(templateFieldAttr)) {
            Set<Long> baseContractIdSet = new HashSet<>();
            List<Map<String, String>> fileList = new ArrayList<>();
            for (PaasFormDataEntityExt paasFormDataEntityExt : formDataEsList) {
                JSONObject data = paasFormDataEntityExt.getData();
                JSONObject jsonObject = data.getJSONObject(templateFieldAttr);
                if(jsonObject==null){
                    continue;
                }
                Long baseContractId = jsonObject.getLong(OnlineEditorConstant.BASE_CONTRACT_ID);
                String contractEditFileId = jsonObject.getString(OnlineEditorConstant.CONTRACT_EDIT_FILE_ID);
                String contractEditFileVersion = jsonObject.getString(OnlineEditorConstant.CONTRACT_EDIT_FILE_VERSION);
                baseContractIdSet.add(baseContractId);
                Map<String, String> tempMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                tempMap.put("fileId", contractEditFileId);
                tempMap.put("fileVersion", contractEditFileVersion);
                fileList.add(tempMap);
            }

            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            params.put("corpid", corpid);
            params.put("idIn", baseContractIdSet);
            List<ContractTemplateBaseEntity> contractTemplateBaseEntityList = contractTemplateBaseModel.findEntities(params);
            if (contractTemplateBaseEntityList != null) {

                contractTemplateBaseEntityList.forEach(item -> {
                    TemplateFieldValuePojo temp = new TemplateFieldValuePojo();
                    temp.setBase_contract_id(item.getId());
                    temp.setBase_contract_name(item.getTemplateName());
                    templateFieldMap.put(item.getId(), temp);
                });
            }


            List<ContractEditFileEntity> contractEditFileEntityList = contractEditFileModel.getByFileIdAndFileVersion(corpid, fileList);
            if (contractEditFileEntityList != null) {
                contractEditFileEntityList.forEach(item -> {
                    String key = item.getFileId() + item.getFileVersion();
                    TemplateFieldValuePojo temp = new TemplateFieldValuePojo();
                    temp.setContract_edit_file_id(item.getFileId());
                    temp.setContract_edit_file_version(item.getFileVersion());
                    temp.setFile_url(item.getFileUrl());
                    temp.setFile_ext(item.getFileExt());
                    temp.setFile_name(item.getFileName());
                    temp.setFile_size(item.getFileSize());
                    templateFieldMap.put(key, temp);
                });
            }
        }

        return templateFieldMap;
    }
}
